package demon1;

import java.util.*;

public class exception {
    public int sqrt(int x) {
        if (x <= 0) {
            return 0;
        }
        int left = 1, right = x;
        while (true) {
            int middle = (left + right) >> 1;
            if (middle <= x / middle && (middle+1) > x / (middle+1)) {
                return (int) middle;
            } else if (middle < x / middle) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }
    }
    public static int removeDuplicates(int[] nums) {
        //双指针方法
        int fast=1;
        int slow=0;
        for(int i=1;i<nums.length+2;i++){
            if(nums[fast]==nums[slow]){
                fast++;
            }else{

                slow++;
                nums[slow]=nums[fast];
            }
        }
        return slow+1;
    }
    public int romanToInt(String s) {
        int sum = 0;
        for (int i=0;i<s.length();i++){
            if(i+1<s.length()&&getValue(s.charAt(i))<getValue(s.charAt(i))){
                sum-=getValue(s.charAt(i));
            }else {
                sum+=getValue(s.charAt(i));
            }
        }
        return sum;
    }

    private int getValue(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }

    public static void main1(String[] args) {
//        String s="aba aa";
//        System.out.println(s.length());
//        System.out.println(s.charAt(s.length()-1));
        int[] arr={0,0,1,1,1,2,2,3,3,4};
        System.out.println(removeDuplicates(arr));
        if(true){
            System.out.println("1");
        }
        System.out.println(14^3);
        ArrayList<Integer> arrayList=new ArrayList<>();
        ArrayList<Integer> arrayList2=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList2.add(21);
        arrayList2.add(2);
        arrayList2.add(1);
        Set<Integer> set=new HashSet(arrayList);
        Set<Integer> set2=new HashSet(arrayList2);
        System.out.println(set2);
        System.out.println(set.retainAll(set2));
        set2.retainAll(set);
        System.out.println(set2);
        set.retainAll(set2);
        System.out.println(set);
        //两种打印方式
       // 1. 增强for循环
        for(int x:set){
            System.out.println(x);
        }
        //2.迭代器获取
        Iterator<Integer> iteratori=set.iterator();
        while(iteratori.hasNext()){
            System.out.println(iteratori.next());
        }

    }

    public static void main2(String[] args) {
        Stack stack =new Stack<>();
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);


        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(2);
        list2.add(2);
        list2.add(2);
        list2.add(2);

        // 保留list1中和list2中相同的元素
        list1.retainAll(list2);
        //System.out.println(list1);

    }
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1 ,ArrayList<Integer> nums2) {
        // write code here
        //利用集合的交集来做
        ArrayList arr=new ArrayList(nums1);
        ArrayList arr2=new ArrayList(nums1);
        arr.retainAll(arr2);
        return arr;

    }

    public static void main3(String[] args) {
        char a='b';
        char c='b';
        System.out.println(a+""+c);
        String re=String.valueOf(a).concat(String.valueOf(c));
        System.out.println(re);
        StringBuffer buffer=new StringBuffer();
        buffer.append(a);
        buffer.append(c);
        String re2=buffer.toString();
        System.out.println(re2);
    }

    //多线程的创建方式
    public static void main4(String[] args) {

    }
    
}
